﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AnaxibiaStudios.Smug;
using System.Reflection;

namespace SmugTest
{
#if DETOUR_ENABLED
  [TestClass]
  public class EventAccessTest
  {
    [TestMethod]
    public void AddTest()
    {
      var cat = Create.Proxy<Cat>();
      var ev = Prepare.Event(cat, (Action sn) => cat.Purred += sn);

      bool purred = false;
      ev.Add(() => { purred = true; });

      Assert.IsFalse(purred);
      ev.Raise();
      Assert.IsTrue(purred);
    }

    [TestMethod]
    public void RemoveTest()
    {
      var cat = Create.Proxy<Cat>();
      var ev = Prepare.Event(cat, (Action sn) => cat.Purred += sn);

      bool purred = false;
      Action setPurr = () => { purred = true; };
      cat.Purred += setPurr;

      Assert.IsFalse(purred);
      ev.Raise();
      Assert.IsTrue(purred);
      purred = false;
      ev.Remove(setPurr);
      ev.Raise();
      Assert.IsFalse(purred);
    }

    [TestMethod]
    public void BasicEventAccessTest()
    {
      var cat = Create.Proxy<Cat>();

      bool purred = false;
      cat.Purred += () => purred = true;

      var purredEvent = Prepare.Event(cat, (Action snatch) => cat.Purred += snatch);

      purredEvent.Raise();

      Assert.IsTrue(purred);
    }

    [TestMethod]
    public void SubscriberAccessTest()
    {
      var cat = Create.Proxy<Cat>();

      var purredEvent = Prepare.Event(cat, (Action snatch) => cat.Purred += snatch);

      Assert.AreEqual(0, purredEvent.Subscribers.Count());

      bool sub0 = false;
      cat.Purred += () => { sub0 = true; };
      Assert.AreEqual(1, purredEvent.Subscribers.Count);

      purredEvent.Raise();
      Assert.IsTrue(sub0);
      sub0 = false;

      bool sub1 = true;
      cat.Purred += () => { sub1 = true; };
      Assert.AreEqual(2, purredEvent.Subscribers.Count);

      purredEvent.Raise();
      Assert.IsTrue(sub0);
      Assert.IsTrue(sub1);
      sub0 = false;
      sub1 = false;

      purredEvent.Subscribers.Clear();
      purredEvent.Raise();
      Assert.IsFalse(sub0);
      Assert.IsFalse(sub1);
    }
  }
#endif
}
